using AppToolKit.Data;
using AppToolKit.Model.Validation.Attribute;
using AppToolKit.Model.Validation.Validator;
using Common.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
namespace AppToolKit.Model.Validation
{
	public class ValidationFactory
	{
		private static readonly ILog logger_ = LogManager.GetLogger(typeof(ValidationFactory));
		private Dictionary<string, ValidationManager> validateLib_;
		private Dictionary<string, List<string>> validateorSourceList_ = new Dictionary<string, List<string>>();
		private IDbProcessor database_;
		private EntityMetaCenter center_;
		public IDbProcessor Database
		{
			get
			{
				return this.database_;
			}
			set
			{
				this.database_ = value;
			}
		}
		public Dictionary<string, ValidationManager> ValidateLib
		{
			get
			{
				if (this.validateLib_ == null)
				{
					this.validateLib_ = new Dictionary<string, ValidationManager>();
				}
				return this.validateLib_;
			}
			set
			{
				this.validateLib_ = value;
			}
		}
		public EntityMetaCenter MetaCenter
		{
			get
			{
				Debug.Assert(this.center_ != null);
				return this.center_;
			}
			set
			{
				this.center_ = value;
			}
		}
		public bool TryRegisted(Type type, Type validateorSource)
		{
			Monitor.Enter(this);
			bool result;
			try
			{
				if (!this.validateorSourceList_.ContainsKey(type.FullName))
				{
					this.validateorSourceList_[type.FullName] = new List<string>();
				}
				List<string> list = this.validateorSourceList_[type.FullName];
				if (list.IndexOf(validateorSource.FullName) > -1)
				{
					ValidationFactory.logger_.Debug(string.Format("{0}尝试注册{1}验证器被拒", type, validateorSource));
					result = false;
				}
				else
				{
					this.validateorSourceList_[type.FullName].Add(validateorSource.FullName);
					ValidationFactory.logger_.Debug(string.Format("{0}尝试注册{1}验证器成功", type, validateorSource));
					result = true;
				}
			}
			finally
			{
				Monitor.Exit(this);
			}
			return result;
		}
		public void Register(Type type, string key, ValidateBehavior behavior, IValidator validator)
		{
			if (!this.ValidateLib.ContainsKey(type.FullName))
			{
				this.ValidateLib[type.FullName] = new ValidationManager(type, this.MetaCenter.GetMeta(type));
			}
			ValidationFactory.logger_.Debug(string.Format("{0}工厂在{1}上提供了{2}进验证的验证器{3}", new object[]
			{
				type,
				key,
				behavior,
				validator
			}));
			this.ValidateLib[type.FullName].Add(key, behavior, validator);
		}
		public IValidator CreateValidator<T>() where T : class, IValidator
		{
			IValidator validator = Activator.CreateInstance<T>();
			if (validator is DataValidator)
			{
				((DataValidator)validator).Database = this.database_;
			}
			return validator;
		}
		public void CollectValidator(Type type)
		{
			if (!this.ValidateLib.ContainsKey(type.FullName))
			{
				this.ValidateLib[type.FullName] = new ValidationManager(type, this.MetaCenter.GetMeta(type));
				PropertyInfo[] properties = type.GetProperties();
				for (int i = 0; i < properties.Length; i++)
				{
					PropertyInfo propertyInfo = properties[i];
					if (propertyInfo.IsDefined(typeof(RuleAttribute), true))
					{
						object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(RuleAttribute), true);
						object[] array = customAttributes;
						for (int j = 0; j < array.Length; j++)
						{
							object obj = array[j];
							IValidator validator = ((RuleAttribute)obj).CreateVaildator();
							if (validator is IDataValidator)
							{
								((IDataValidator)validator).Database = this.Database;
							}
							ValidationFactory.logger_.Debug(string.Format("注册验证器(属性{0},行为{1},类型{2})", propertyInfo.Name, ((RuleAttribute)obj).Behavior, validator));
							this.ValidateLib[type.FullName].Add(propertyInfo.Name, ((RuleAttribute)obj).Behavior, validator);
						}
					}
				}
			}
		}
		public ValidateResultSet Validate(Type type, object entity, ValidateBehavior behavior)
		{
			string fullName = type.FullName;
			ValidateResultSet result;
			if (this.ValidateLib.ContainsKey(fullName))
			{
				result = this.ValidateLib[fullName].Validate(entity, behavior);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public void Register(Type type, string key, IValidator validator)
		{
			this.Register(type, key, ValidateBehavior.Both, validator);
		}
	}
}
